import threading
import queue
import random
import time

'''
线程间的通信queue（生产者与消费者）
'''


#生产者
def produce(g):
    i = 0
    while i < 15:
        #num = random.randint(1,100)
        num = i
        q.put("生产者产生数据：%d" % num)
        print("生产者产生数据：%d" % num)
        time.sleep(1)
        i += 1
    q.put(None)
    q.task_done()
    
#消费者
def consume(g):
    i = 0
    while True:
        item = q.get()
        if item is None:
            break
        print("消费者产生数据：%s" % item)
        time.sleep(4)
    q.task_done()
    
    
if __name__ == '__main__':
    q = queue.Queue(10)
    arr = []
    t1 = threading.Thread(target=produce,args=(q,))
    t2 = threading.Thread(target=consume,args=(q,))
    
    t2.start()
    t1.start()
    
    
    t2.join()
    t1.join()
    